<?php

namespace common\models\base;

use Yii;

/**
 * This is the model class for table "{{%car_order}}".
 *
 * @property int $id
 * @property int $custom_id
 * @property int $status 状态
 * @property string $cust_name 客户名
 * @property string $id_type 证件类型
 * @property string $id_no 证件号
 * @property string $serialize_data 其他数据
 * @property int $car_type 类型10：一手车，11：二手车
 *
 * @property Custom $custom
 * @property CarOrderLog[] $carOrderLogs
 * @property CarOrderLog$lastCarOrderLog
 */
class CarOrder extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%car_order}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['custom_id', 'cust_name', 'id_no'], 'required'],
            [['custom_id', 'status', 'car_type'], 'integer'],
            [['serialize_data'], 'string'],
            [['cust_name', 'id_no'], 'string', 'max' => 20],
            [['id_type'], 'string', 'max' => 10],
            [['id_no', 'id_type'], 'unique', 'targetAttribute' => ['id_no', 'id_type']],
            [['custom_id'], 'exist', 'skipOnError' => true, 'targetClass' => Custom::className(), 'targetAttribute' => ['custom_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'custom_id' => Yii::t('app', 'Custom ID'),
            'status' => Yii::t('app', '状态'),
            'cust_name' => Yii::t('app', '客户名'),
            'id_type' => Yii::t('app', '证件类型'),
            'id_no' => Yii::t('app', '证件号'),
            'serialize_data' => Yii::t('app', '其他数据'),
            'car_type' => Yii::t('app', '类型10：一手车，11：二手车'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCustom()
    {
        return $this->hasOne(Custom::className(), ['id' => 'custom_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCarOrderLogs()
    {
        return $this->hasMany(CarOrderLog::className(), ['car_order_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getLastCarOrderLog()
    {
        return $this->hasOne(CarOrderLog::className(), ['car_order_id' => 'id'])->orderBy(['id' => SORT_DESC]);
    }
}
